/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hu.home.leva.db.users;

import hu.home.leva.db.DataBase;
import hu.home.leva.db.Entity;
import hu.home.leva.db.IManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author leva
 */
public class UserManager implements IManager {

    private DataBase database = DataBase.getInstance();
    private static UserManager usermanager=null;
   
    @Override
    public List<Entity> getAll() {
        List<Entity> entities = new ArrayList<Entity>();
        try {
            Statement statement = database.getConnection().createStatement();
            ResultSet resultset;
            String selectUsers ="select * from user;";
            resultset = statement.executeQuery(selectUsers);
            while(resultset.next()){
                int id = resultset.getInt("id");
                String nickname= resultset.getString("nickname");
                String email= resultset.getString("email");
                String password= resultset.getString("password");
                User user = new User(id,nickname,email,password);
                entities.add(user);
                
            }
            
        } catch (SQLException ex) {
            Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return entities; 
    }

    @Override
    public Entity getById(int byid) {
        User user=null;
        try{
            String selectById ="select * from user where user.id = ?;";
            PreparedStatement preparedstatement = database.getConnection().prepareStatement(selectById);
            preparedstatement.setInt(1, byid);
            ResultSet resultset = preparedstatement.executeQuery();
            while(resultset.next()){
                int id = resultset.getInt("id");
                String nickname= resultset.getString("nickname");
                String email= resultset.getString("email");
                String password= resultset.getString("password");
                user = new User(id,nickname,email,password);
            }
        }
        catch(Exception ex){
            Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return user;
    }
    
    public Entity getByEmail(String byEmail) {
        User user=null;
        try{
            String selectByEmail ="select * from user where user.email = ?;";
            PreparedStatement preparedstatement = database.getConnection().prepareStatement(selectByEmail);
            preparedstatement.setString(1, byEmail);
            ResultSet resultset = preparedstatement.executeQuery();
            while(resultset.next()){
                int id = resultset.getInt("id");
                String nickname= resultset.getString("nickname");
                String email= resultset.getString("email");
                String password= resultset.getString("password");
                user = new User(id,nickname,email,password);
            }
        }
        catch(Exception ex){
            Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        return user;
    }

    @Override
    public void saveAll(List<Entity> entities) {
        List<User> users = new ArrayList<User>();
        users = (List<User>) (User) entities;
        for(User user:users){
            save(user);
        }
    }
    
    private void insert(Entity entity){
        User user =(User) entity;
        try{
            String insertUser = "insert into user(nickname,email,password) values (?,?,?);";
            PreparedStatement preparedstatement = database.getConnection().prepareStatement(insertUser);
            preparedstatement.setString(1, user.getNickname());
            preparedstatement.setString(2, user.getEmail());
            preparedstatement.setString(3, user.getPassword());
            preparedstatement.executeUpdate();
        }
        catch(Exception ex){
            Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
        }   
    }
    
    private void update(Entity entity){
        User user =(User) entity;
        try{
            String updateUser = "update user set nickname=?,email=?,password=? where id= "+entity.getId()+";";
            PreparedStatement preparedstatement = database.getConnection().prepareStatement(updateUser);
            preparedstatement.setString(1, user.getNickname());
            preparedstatement.setString(2, user.getEmail());
            preparedstatement.setString(3, user.getPassword());
            preparedstatement.executeUpdate();
        }
        catch(Exception ex){
            Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
        }
   
    }

    @Override
    public void save(Entity entity) {
         if(entity.getId()==-1){
            insert(entity);
        }
        else{
            update(entity);
        }
    }

    @Override
    public void delete(Entity entity) {
        try{
            String deleteById ="delete from user where id = ?;";
            PreparedStatement preparedstatement = database.getConnection().prepareStatement(deleteById);
            preparedstatement.setInt(1, entity.getId());
            preparedstatement.executeUpdate(deleteById);
            
        }
        catch(Exception ex){
            Logger.getLogger(UserManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void deleteAll(List<Entity> entities) {
        List<User> users = new ArrayList<User>();
        users = (List<User>) (User) entities;
        for(User user:users){
            delete(user);
        }
    }
    
    public static synchronized  UserManager getInstance(){
        if (usermanager==null) usermanager= new UserManager();
        
        return usermanager;
    }
       
}
